package com.itheima.reflect;


import org.junit.jupiter.api.Test;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;

public class MybatisUtil {
    @Test
     public  void save(Object o)  {
        try(PrintStream ps = new PrintStream(new FileOutputStream("D:/JavaSE/Reflect/src/data.txt",true));)
        {
        //1.获取class文件
        Class aClass = o.getClass();
        //有两个方法获取类的名字，或者类的全名
        ps.println("------"+aClass.getSimpleName()+"------");
        //2. 提取全部变量
        Field[] declaredFields = aClass.getDeclaredFields();
        for(Field field: declaredFields){
            String name = field.getName();
            String value = field.get(o)+"";
            ps.println(name+"="+value);
        }
    } catch(Exception e){
            e.printStackTrace();
        }
    }
}
